<template>
	<view class="container">
		<!-- 待办事项 -->
		<view class="todo-section">
			<view class="section-header">
				<text class="section-title">待办事项</text>
			</view>

			<view class="todo-grid">
				<!-- 第一行4个 -->
				<view class="todo-row">
					<view class="todo-item" @click="navigateTo('pendingReview')">
						<image src="/static/icons/review.png" class="todo-icon" />
						<text class="todo-number">{{ reviewCount }}</text>
						<text class="todo-text">待审核</text>
					</view>
					<view class="todo-item" @click="navigateTo('pendingReceive')">
						<image src="/static/icons/receive.png" class="todo-icon" />
						<text class="todo-number">{{ receiveCount }}</text>
						<text class="todo-text">待签收</text>
					</view>
					<view class="todo-item" @click="navigateTo('pendingInventory')">
						<image src="/static/icons/inventory.png" class="todo-icon" />
						<text class="todo-number">{{ inventoryCount }}</text>
						<text class="todo-text">待盘点</text>
					</view>
					<view class="todo-item" @click="navigateTo('pendingRepair')">
						<image src="/static/icons/repair.png" class="todo-icon" />
						<text class="todo-number">{{ repairCount }}</text>
						<text class="todo-text">待维修</text>
					</view>
				</view>

				<!-- 第二行4个 -->
				<!-- 				<view class="todo-row">

					<view class="todo-item" @click="navigateTo('pendingMaintain')">
						<image src="/static/icons/maintain.png" class="todo-icon" />
						<text class="todo-number">{{ maintainCount }}</text>
						<text class="todo-text">待保养</text>
					</view>
					<view class="todo-item" @click="navigateTo('pendingReturn')">
						<image src="/static/icons/return.png" class="todo-icon" />
						<text class="todo-number">{{ returnCount }}</text>
						<text class="todo-text">待归还</text>
					</view>
					<view class="todo-item" @click="navigateTo('subscription')">
						<image src="/static/icons/notice.png" class="todo-icon" />
						<text class="todo-number">{{ subscriptionCount }}</text>
						<text class="todo-text">订阅通知</text>
					</view>
				</view> -->
			</view>
		</view>

		<!-- 分割线 -->
		<view class="divider"></view>

		<!-- 个人资产 -->
		<view class="todo-section">
			<view class="section-header">
				<text class="section-title">个人资产</text>
			</view>
		</view>
		<!-- 个人资产 -->
		<view class="personal-assets">
			<view class="asset-item">
				<image src="/static/icons/maintain.png" class="todo-icon" />
				<text class="asset-text">我的资产: ({{ myAssetCount }}个资产)</text>
			</view>
			<view class="asset-item">
				<image src="/static/icons/maintain.png" class="todo-icon" />
				<text class="asset-text">我发起的: ({{ myRequestCount }}个单据)</text>
			</view>
		</view>

		<!-- 返回按钮 -->
		<view class="footer">
			<button class="back-button" @click="goBack">返回</button>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		onMounted
	} from 'vue';
	import {
		onLoad
	} from '@dcloudio/uni-app';
	import http from '@/api/request';
	import {
		generateUUID
	} from "@/utils";

	// 定义响应式变量
	const reviewCount = ref(0);
	const receiveCount = ref(0);
	const inventoryCount = ref(0);
	const repairCount = ref(0);
	const myAssetCount = ref(0);
	const myRequestCount = ref(0);
	// auditStatusList 结构如下
	// [
	//     {
	//         "children": [],
	//         "filter": "A,B,C,D",
	//         "id": "191386b3-cd4e-42a2-8c64-f80f037095fc",
	//         "isRoot": false,
	//         "parentId": "ec19008b-d933-41b5-960d-fe0f9d4fc493",
	//         "text": "待审核",
	//         "value": "PENDING_REVIEW"
	//     },
	//     {
	//         "children": [],
	//         "filter": "A,B",
	//         "id": "6331b1f3-2f03-4d2d-9eb1-6aeb38bf8f5a",
	//         "isRoot": false,
	//         "parentId": "ec19008b-d933-41b5-960d-fe0f9d4fc493",
	//         "text": "审核中",
	//         "value": "UNDER_REVIEW"
	//     },
	//     {
	//         "children": [],
	//         "filter": "A,B,C,D",
	//         "id": "a901172f-035d-4ccf-82fa-5f0d2fa374a4",
	//         "isRoot": false,
	//         "parentId": "ec19008b-d933-41b5-960d-fe0f9d4fc493",
	//         "text": "审核通过",
	//         "value": "APPROVED"
	//     },
	//     {
	//         "children": [],
	//         "filter": "A,B,C,D",
	//         "id": "52fa23cf-c57a-4c34-9ca4-8ef288025008",
	//         "isRoot": false,
	//         "parentId": "ec19008b-d933-41b5-960d-fe0f9d4fc493",
	//         "text": "驳回",
	//         "value": "REJECTED"
	//     },
	//     {
	//         "children": [],
	//         "filter": "A,C",
	//         "id": "4940130c-5b08-4167-b4ab-5088c03a8a19",
	//         "isRoot": false,
	//         "parentId": "ec19008b-d933-41b5-960d-fe0f9d4fc493",
	//         "text": "完成",
	//         "value": "COMPLETED"
	//     }
	// ]
	let auditStatusList = [];

	// 获取待办事项数据
	const fetchTodoData = async () => {
		try {
			// 这里替换为实际的API调用
			let reqParam = {
				"pageCode": "assetlist",
				"params": {
					"args": [{
						"name": "interfaceCode",
						"value": "Assetlist.todoCount"
					}]
				},
				"methodName": "todoCount"
			}
			const response = await http.post('/api/module/Invoke', reqParam);

			if (response.isSuccess && response.result) {
				const data = response.result;
				// 更新数据
				reviewCount.value = data.pending_review_num || 0;
				receiveCount.value = data.sign_num || 0;
				inventoryCount.value = data.stocktaking_num || 0;
				repairCount.value = data.repair_num || 0;
			}
		} catch (error) {
			console.error('获取待办事项数据失败:', error);
			// 可以设置默认值或显示错误提示
			uni.showToast({
				title: '获取数据失败',
				icon: 'none'
			});
		}
	};

	// 获取我的资产数据
	const fetchMyAssetData = async () => {
		try {
			// 这里替换为实际的API调用
			let reqParam = {
				"pageCode": "assetlist",
				"params": {
					"args": [{
						"name": "interfaceCode",
						"value": "Assetlist.myAsset"
					}]
				},
				"methodName": "myAsset"
			}
			const response = await http.post('/api/module/Invoke', reqParam);

			if (response.isSuccess && response.result) {
				const data = response.result;
				// 更新数据
				myAssetCount.value = data.my_asset_num || 0;
				myRequestCount.value = data.my_send_num || 0;
			}
		} catch (error) {
			console.error('获取我的资产数据失败:', error);
			// 可以设置默认值或显示错误提示
			uni.showToast({
				title: '获取数据失败',
				icon: 'none'
			});
		}
	};

	// 导航到不同页面
	const navigateTo = (type) => {
		let url = '';
		switch (type) {
			case 'pendingReview':
				const pendingReviewItem = auditStatusList.find(item => item.value === "PENDING_REVIEW");
				url = '/pages/assetsPages/list?tabs=1&statusObj=' + JSON.stringify(pendingReviewItem);
				break;
			case 'pendingReceive':
				const pendingReceiveItem = auditStatusList.find(item => item.value === "APPROVED");
				url = '/pages/assetsPages/list?tabs=2&statusObj=' + JSON.stringify(pendingReceiveItem);
				break;
			case 'pendingInventory':
				url = '/pages/stocktaking/stocktaking';
				break;
			case 'pendingRepair':
				url = '/pages/assetsPages/list?tabs=6';
				break;
			default:
				url = '/pages/todo/list';
		}
		// 跳转盘点页方式不一样
		if ('pendingInventory' == type) {
			// 由于 switchTab 不能直接传递参数到目标页面（Tab 页面切换时不会触发 onLoad），
			// 使用本地存储来传递数据。
			uni.setStorageSync("inventoryStatus", "NOT_STARTED");
			uni.switchTab({
				url: url,
			});
		} else {
			uni.navigateTo({
				url
			});
		}
	};

	// 获取基础数据-审核状态对象集合
	const fetchAuditStatusList = async () => {

		const params = [{
			id: generateUUID(),
			service: "Module",
			method: "getBasicData",
			param: JSON.stringify({
				code: "common>approve_status",
				filters: []
			})
		}];

		await http.post("/api/module/GetBatchResult", params).then((res) => {
			auditStatusList = res.result[0].response.data.data
		});
	};

	// 返回上一页
	const goBack = () => {
		uni.navigateBack();
	};

	onLoad(() => {
		uni.setNavigationBarTitle({
			title: '待办事项'
		});
		// 待办统计数据
		fetchTodoData();
		// 我的资产统计数据
		fetchMyAssetData();
		// 获取基础数据-审核状态对象集合
		fetchAuditStatusList();

	});
</script>

<style lang="scss" scoped>
	.container {
		padding: 20rpx;
		background-color: #f7f7f7;
		min-height: 100vh;
	}

	.section-header {
		margin-bottom: 20rpx;
		padding: 20rpx;
		background-color: #fff;
		border-radius: 12rpx;
	}

	.section-title {
		font-size: 36rpx;
		font-weight: bold;
		color: #333;
	}

	.todo-grid {
		background-color: #fff;
		border-radius: 12rpx;
		padding: 20rpx;
		margin-bottom: 20rpx;
	}

	.todo-row {
		display: flex;
		justify-content: space-between;
		margin-bottom: 30rpx;

		&:last-child {
			margin-bottom: 0;
		}
	}

	.todo-icon {
		width: 80rpx;
		height: 80rpx;
		margin-bottom: 10rpx;
	}

	.todo-item {
		display: flex;
		flex-direction: column;
		align-items: center;
		width: 22%;



		.todo-number {
			font-size: 36rpx;
			font-weight: bold;
			color: #4792fd;
			margin-bottom: 5rpx;
		}

		.todo-text {
			font-size: 26rpx;
			color: #666;
		}
	}

	.divider {
		height: 1rpx;
		background-color: #eee;
		margin: 30rpx 0;
	}

	.personal-assets {
		background-color: #fff;
		border-radius: 12rpx;
		padding: 20rpx;

		.asset-item {
			display: flex;
			align-items: center;
			padding: 25rpx 0;
			border-bottom: 1rpx solid #f5f5f5;

			&:last-child {
				border-bottom: none;
			}

			.asset-checkbox {
				margin-right: 20rpx;
				transform: scale(0.9);
			}

			.asset-text {
				font-size: 28rpx;
				color: #333;
			}
		}
	}

	.footer {
		margin-top: 40rpx;
		text-align: center;

		.back-button {
			background-color: #4792fd;
			color: #fff;
			padding: 15rpx 40rpx;
			font-size: 28rpx;
			display: inline-block;
			border: none;
			outline: none;
			width: 100%;
		}
	}
</style>